﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FrmHotel
{
    public partial class FrmYvDing : Form
    {
        public FrmYvDing()
        {
            InitializeComponent();
        }
        public int id;//接收主菜单的房间号 
        private void FrmYvDing_Load(object sender, EventArgs e)
        {
            ComdoBox();
            BoinDataGridView();
            string sql = string.Format("select r.RoomName from Room r,RoomInfo o where o.RmRoom=r.RoomId and o.RmId={0}", id);
            string r = DBHelper.SelectOne(sql).ToString();
            this.textBox3.Text = r;
            string sql1 = string.Format("select r.RoomMoney from Room r,RoomInfo o where o.RmRoom=r.RoomId and o.RmId={0}", id);
            string r1 = DBHelper.SelectOne(sql1).ToString();
            this.txt_feiyong.Text = r1;
        }
        public void ComdoBox()
        {
            string sql = "select * from  CetaeClass";
            DataTable dt = DBHelper.SelectForTable(sql);
            DataRow dr = dt.NewRow();
            dr["CetaeId"] = 0;
            dr["CetaeName"] = "请选择";
            dt.Rows.InsertAt(dr, 0);
            this.comboBox1.DisplayMember = "CetaeName";
            this.comboBox1.ValueMember = "CetaeId";
            this.comboBox1.DataSource = dt;

            string sql2 = "select * from  PayClass";
            DataTable dt2 = DBHelper.SelectForTable(sql2);
            DataRow dr2 = dt2.NewRow();
            dr2["PayId"] = 0;
            dr2["PayName"] = "请选择";
            dt2.Rows.InsertAt(dr2, 0);
            this.comboBox3.DisplayMember = "PayName";
            this.comboBox3.ValueMember = "PayId";
            this.comboBox3.DataSource = dt2;

        }
        public void BoinDataGridView()
        {
            this.dataGridView1.AutoGenerateColumns = false;
            string sql = "select r.RmId,v.RvName,v.RvPhone,v.RvYvDing,y.YzSatseName from ResvInfo v,YzSatse y,RoomInfo r where  r.RmId=v.RvFangHao and v.RvSatse=y.YzSatseId  and 1=1";
            string hao = this.textBox4.Text;
            if (!string.IsNullOrEmpty(hao))
            {
                sql += string.Format(" and v.RvName like '%{0}%' ", hao);
            }
            string name = this.textBox5.Text;
            if (!string.IsNullOrEmpty(name))
            {
                sql += string.Format(" and r.RmId  like '%{0}%' ", name);
            }
            this.dataGridView1.DataSource = DBHelper.SelectForTable(sql);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string name = this.txt_name.Text;//宾客名称
            string gender = this.radioButton1.Checked ? "1" : "0";//性别
            int age = Convert.ToInt32(this.numericUpDown1.Value);//年龄
            int zhengjian = Convert.ToInt32(this.comboBox1.SelectedValue);//证件类型
            string bianhaoH = this.txt_zhengjainhao.Text;//证件号
            string Address = this.txt_Address.Text;//住址
            string hoper = this.txt_hoper.Text;//联系电话
            decimal money = Convert.ToDecimal(this.txt_feiyong.Text);//房间金额
            string yvdiSJ = this.dateTimePicker1.Value.ToString("yyy-MM-dd 12:00:00");//预抵时间
            string yvliSJ = this.dateTimePicker2.Value.ToString("yyy-MM-dd 12:00:00");//预离时间
            int fnagshi = Convert.ToInt32(this.comboBox3.SelectedValue);//收款方式

            string sql5 = string.Format("select count(1) from ResvInfo r,RoomInfo m where r.RvHao='{0}' and r.RvAdass='{1}' and r.RvPhone='{2}' and m.RmId={3}", bianhaoH, Address, hoper,id);
            int re1 = Convert.ToInt32(DBHelper.SelectOne(sql5));
            if (re1 >= 1)
            {
                MessageBox.Show("此宾客已预住！", "提示信息！");
                return;
            }
           
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(hoper) || string.IsNullOrEmpty(bianhaoH) || string.IsNullOrEmpty(Address))
            {
                MessageBox.Show("请填写完整的入住信息！", "提示信息！");
                return;
            }

            DateTime yvdi = this.dateTimePicker1.Value;//预抵时间
            DateTime yvli = this.dateTimePicker2.Value;//预离时间
            DateTime dt = DateTime.Now;

            string cha = string.Format("select DATEDIFF(DAY,'{0}','{1}')", yvdi, yvli);
            int result1 = Convert.ToInt32(DBHelper.SelectOne(cha));
            decimal he = Convert.ToDecimal(result1 * money);

            if (yvdi<=dt)
            {
                MessageBox.Show("预抵时间不能是当天！","提示信息！");
                return;
            }
            else if (yvdi == yvli)
            {
                MessageBox.Show("预离时间不能是预抵当天！", "提示信息！");
            }
            else if (yvdi > yvli)
            {
                MessageBox.Show("预离时间不能是小于预抵当天！", "提示信息！");
            }
            else
            {
                DateTime tt = DateTime.Now;//当前系统日期 
                string sql = string.Format(@"insert into resvinfo values('{0}','{1}',{2},{3},'{4}','{5}','{6}','{7}','{8}',default,{9},'{10}',{11},{12},{13})",
                                                              name, gender, age, zhengjian, bianhaoH, hoper,tt, yvdiSJ, yvliSJ, he, Address,fnagshi, id, 2);
                int result = Convert.ToInt32(DBHelper.ExecuteUpdate(sql));
                if (result > 0)
                {
                    MessageBox.Show("预住成功！", "提示信息！");
                   BoinDataGridView();
                }
                else
                {
                    MessageBox.Show("预住失败！", "提示信息！");
                    BoinDataGridView();
                    return;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            BoinDataGridView();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            BoinDataGridView();
        }

    }
}
